﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
using System.Data;
public partial class Administrator_ProductEdit : System.Web.UI.Page
{
    BindToControls objBind = new BindToControls();
    ConnectSQL cnts = new ConnectSQL();
    BizProduct objProduct = new BizProduct();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropdownlist();
            SearchData();
        }
    }
    string PRODUCT_ID
    {
        get
        {
            return Request.QueryString["PRODUCT_ID"].ToString();
        }
    }
    public void SearchData()
    {
        DataTable db = cnts.GetTableWithCommandText("select * from tblProducts where PRODUCT_ID=" + PRODUCT_ID);

        txtContent.Text = db.Rows[0]["DETAIL"].ToString();
        txtPrice.Text = db.Rows[0]["PRICE"].ToString();


        txtProductName.Text = db.Rows[0]["PRODUCT_NAME"].ToString();
        txtPrice.Text = db.Rows[0]["PRICE"].ToString();
        string image_Des = db.Rows[0]["IMAGE_DES"].ToString();
        string image_detail = db.Rows[0]["IMAGE_DETAIL"].ToString();
        string free = db.Rows[0]["IS_FREE"].ToString();
        if (free == "1")
        { cbFree.Checked = true; }
        string is_best_selling = db.Rows[0]["IS_BEST_SELLING"].ToString();
        if (is_best_selling == "1")
        {
            cbBestSelling.Checked = true;
        }


        string is_highvalue = db.Rows[0]["IS_HIGH_VALUED"].ToString();
        if (is_highvalue == "1")
        {
            cbHighValue.Checked = true;
        }


        string is_New_Published = db.Rows[0]["IS_NEWS_PUBLISHED"].ToString();
        if (is_New_Published == "1")
        {
            cbNew.Checked = true;
        }



        txtSortDisplay.Text = db.Rows[0]["DISPLAY_ORDER"].ToString();
        string status = db.Rows[0]["STATUS"].ToString();
        if (status == "1")
        {
            cbStatus.Checked = true;
        }
        string language = db.Rows[0]["LANGUAGE"].ToString();
        ddlLanguage.SelectedValue = language;

        string publish_id = db.Rows[0]["PUBLISHER_ID"].ToString();
        ddlPublisher.SelectedValue = publish_id;
        string published_date = db.Rows[0]["PUBLISHED_DATE"].ToString();

        txtPublishedDate.Text = LinktamFormat.FormatDateEnglish(published_date);
        ddlCategory.SelectedValue = db.Rows[0]["CATEGORY_ID"].ToString();



    }
    public void BindDropdownlist()
    {
        objBind.BinDDL(ddlCategory, cnts.GetTableWithCommandText("select * from tblProductCategories"), "CATEGORY_NAME", "CATEGORY_ID");
        objBind.BinDDL(ddlPublisher, cnts.GetTableWithCommandText("select * from tblPublish"), "PUBLISHER_NAME", "PUBLISHER_ID");
    }
    BizProductCategory category = new BizProductCategory();
    public int GetRandom()
    {
        Random rd = new Random();
        int a = rd.Next(1, 100);
        return a;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        string status = "0";
        if (cbStatus.Checked)
        {
            status = "1";
        }
        else
            status = "0";

        int bestSelling = 0;

        if (cbBestSelling.Checked)
        {
            bestSelling = 1;
        }
        int free = 0;
        if (cbFree.Checked)
        {
            free = 1;
        }
        int highvalue = 0;
        if (cbHighValue.Checked)
        {
            highvalue = 1;
        }
        int isnew = 0;
        if (cbNew.Checked)
        {
            isnew = 1;
        }

        try
        {

            DataTable db = cnts.GetTableWithCommandText("select * from tblProducts where PRODUCT_ID=" + PRODUCT_ID);



            string file_doc = db.Rows[0]["FILE_DOWNLOAD"].ToString();


            if (fuDoc.HasFile)
            {
                file_doc = GetRandom() + "" + fuDoc.FileName;
                fuDoc.SaveAs(Server.MapPath("~/TaiLieu/") + file_doc);
            }

            string des_file = db.Rows[0]["FILE_DESCRIPTION"].ToString();
            if (fuDeCuong.HasFile)
            {
                des_file = GetRandom() + "" + fuDeCuong.FileName;
                fuDeCuong.SaveAs(Server.MapPath("~/TaiLieu/") + des_file);
            }



            int product_id = int.Parse(PRODUCT_ID);

            objProduct.tblProducts_Update(product_id, txtProductName.Text, int.Parse(ddlPublisher.SelectedValue),
                int.Parse(ddlCategory.SelectedValue), DateTime.Parse(txtPublishedDate.Text), ddlLanguage.SelectedItem.Text,
                float.Parse(txtPrice.Text), string.Empty, string.Empty, free, bestSelling, highvalue, isnew, txtSortDisplay.Text, status, string.Empty, txtContent.Text, file_doc, des_file);


            divMessage.InnerHtml = O2S_Message.Success("Hệ thống", "Thực hiện thành công");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ thống", "Phát hiện lỗi" + ex.Message);
        }

    }
}